package com.ithero.usercenter.rocketmq;

import com.ithero.usercenter.domain.dto.messaging.UserAddBonusMsgDTO;
import com.ithero.usercenter.service.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.ErrorMessage;
import org.springframework.stereotype.Service;

/**
 * Stream Rocket编程模型消费者监听
 */
@Slf4j
@Service
public class AddBonusStreamConsumer {

    @Autowired
    private UserService userService;

    @StreamListener(Sink.INPUT)
    public void receive(UserAddBonusMsgDTO message) {
        message.setEvent("CONTRIBUTE");
        message.setDescription("投稿加积分...");
        // 当收到消息的时候执行的业务
        userService.addBonus(message);

        //throw new IllegalArgumentException("测试全局异常");
    }

    /**
     * 处理全局异常
     */
    @StreamListener("errorChannel")
    public void error(Message<?> message) {
        ErrorMessage errorMessage = (ErrorMessage) message;
        log.warn("发生了异常，errorMessage = {}", errorMessage);
    }
}
